import {KaseoRuntimeBridge} from "../KaseoRuntime";
import {KaseoCommon2dGDI} from "../dev/KaseoCanvasGraphicsDI";
import LayoutBase from "./layout/LayoutBase";

export default class UIPage {
    private layout: LayoutBase

    targetGdiIdx: 0 | 1

    __$$page_quit: boolean = false

    constructor(layout: LayoutBase, targetGdiIdx: 0 | 1) {
        this.layout = layout
        this.targetGdiIdx = targetGdiIdx
    }

    onAwakeRender(bridge: KaseoRuntimeBridge) {
        const gdi = this.targetGdiIdx == 0 ? 
                    bridge.getMajorGraphicsGDI() :
                    bridge.getExtraGraphicsGDI()

        if (gdi != null) {
            this.layout.__$$firstRender(gdi, {
                preferHeight: gdi.getScreenHeight(),
                preferWidth: gdi.getScreenWidth()
            })

            this.layout.__$$render(gdi, {
                preferHeight: gdi.getScreenHeight(),
                preferWidth: gdi.getScreenWidth()
            })
        }
    }

    onKnock(bridge: KaseoRuntimeBridge) {
        const flag = { shouldRender: false }
        this.layout.__$$onKnock(bridge, flag)
        
        const gdi = this.targetGdiIdx == 0 ? 
                    bridge.getMajorGraphicsGDI() :
                    bridge.getExtraGraphicsGDI()

        if (gdi != null && flag.shouldRender) {
            this.layout.__$$render(gdi, {
                preferHeight: gdi.getScreenHeight(),
                preferWidth: gdi.getScreenWidth()
            })
        }
    }

    onUpdate(bridge: KaseoRuntimeBridge) {
        const flag = { shouldRender: false }
        this.layout.__$$onUpdate(bridge, flag)
                
        const gdi = this.targetGdiIdx == 0 ? 
                    bridge.getMajorGraphicsGDI() :
                    bridge.getExtraGraphicsGDI()

        if (gdi != null && flag.shouldRender) {
            this.layout.__$$render(gdi, {
                preferHeight: gdi.getScreenHeight(),
                preferWidth: gdi.getScreenWidth()
            })
        }
    }
}
